using System;
using Portal.Core;
using Portal.Core.Utils;
using Portal.Data;
using Portal.Data.Helper;
using Page = Portal.Data.Entity.Page;

public partial class Admin_Manager : System.Web.UI.Page
{
    private const string PATH_CONFIG_FILE = "~/Admin/Config/";

    protected void Page_Load(object sender, EventArgs e)
    {
        //check is access
        if (!IsPostBack)
        {
            if (PortalContext.CurrentUser != null)
                SelectedWorking();
            else
                Response.Redirect(CoreContext.basepath + "admin_login.aspx");
        }
    }

    private void SelectedWorking()
    {
        if (!string.IsNullOrEmpty(Request.QueryString["insert"]))
        {
            phCreatNew.Visible = true;
        }
        else
        {
            phCreatNew.Visible = false;
        }
        GridView1.DataBind();
    }

    #region Config file

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Manager.aspx?insert=1");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Manager.aspx");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            var path = Server.MapPath(PATH_CONFIG_FILE + NameNew.Text + ".config");
            string sContent = MyFile.ReadTextFromFile(path);
            var newpage = new Page();
            newpage.Name = NameNew.Text;
            newpage.Content = sContent;
            ORMHelper.InstanceIgnorePermission.Save(newpage);
            Response.Redirect(Request.RawUrl);
        }
        catch (Exception ex)
        {
            lbErrorConfig.Text = string.Format("<b>{0}</b>", ex.Message);
        }
    }

    #endregion
}